#include "../server.hpp"
class EchoServer {
private:
    TcpServer _server;
private:
    void OnConnected(const PtrConnection& conn){
    DBG_LOG("NEW CONNECTION: %p",conn.get());
    }
    void OnMessage(const PtrConnection& conn,Buffer* buf){
        // DBG_LOG("%s",buf->ReadPosition());
        // buf->MoveReadOffset(buf->ReadAbleSize());
        // std::string str = "hello world";
        conn->Send(buf->ReadPosition(),buf->ReadAbleSize());
        buf->MoveReadOffset(buf->ReadAbleSize());
        conn->Shutdown();
    }
    void OnClosed(const PtrConnection& conn){
        DBG_LOG("Close CONNECTION: %p",conn.get());
    }
public:
    EchoServer(int port):_server(port) {
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(10);
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected,this,std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
        _server.SetClosedCallback(std::bind(&EchoServer::OnClosed,this,std::placeholders::_1));
    }
    void Start() {
        _server.Start();
    }
};